using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using BookDownloader.Core.Behaviour;
using BookDownloader.Core.Data;
using BookDownloader.Core.Implementation;
using BookDownloader.Core.Implementation.Flibusta;
using Microsoft.Practices.ObjectBuilder2;
using Microsoft.Practices.Unity;

namespace BookDownloader.Core
{
	public class RatesConstrOverride:ResolverOverride
	{
		public override IDependencyResolverPolicy GetResolver(IBuilderContext context, Type dependencyType)
		{
			throw new NotImplementedException();
		}
	}
	[Serializable]
	public class Project : IProject
	{
		private IRatesProvider ratesProvider;
		private IContentProvider contentProvider;
		private ILibraryListProvider libraryProvider;
		private IBookMapper mapper;

		List<LibraryBook> library = new List<LibraryBook>();
		List<MappedBook> myBooks = new List<MappedBook>();
		public IList<LibraryBook> Library
		{
			get { return library; }
		}

		public IList<MappedBook> MyBooks
		{
			get { return myBooks; }
		}

		public Project()
		{
			LoadProjectItems();
		}

		public void LoadProjectItems()
		{
			//ratesProvider = new FantlabFileRatesProvider(myBooks);
			//contentProvider = new FlibustaContentProvider(new ConfigPathProvider());
			//libraryProvider = new FlibustaLibraryProvider(library);
			//mapper = new BookMapper();
			ratesProvider = DiFacade.Container.Resolve<IRatesProvider>(new ParameterOverride("myBooks",MyBooks));
			contentProvider = DiFacade.Container.Resolve<IContentProvider>();
			libraryProvider = DiFacade.Container.Resolve<ILibraryListProvider>(new ParameterOverride("library",Library));
			mapper = new BookMapper();
		}

		public void LoadRates(bool restoreLastVersion)
		{
			if (restoreLastVersion)
			{
				ratesProvider.LoadCachedRates();
			}
			else
			{
				ratesProvider.RefreshRates();
			}
		}

		public void LoadList(bool restoreLastVersion)
		{
			if (restoreLastVersion)
			{
				libraryProvider.LoadCachedList();
			}
			else
			{
				libraryProvider.RefreshList();
			}
		}

		public void Map(bool unmappedOnly, int maxPos)
		{
			mapper.MapBook(MyBooks, Library, maxPos);
		}

		public void Generate(int count)
		{
			contentProvider.CreateLibrary(myBooks,FileFormatFactory.Fb2, count);
		}


	}
}